(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:OrganizeCtrl
     * @description
     * # OrganizeCtrl
     * Controller of the webApp
     */
    angular.module('webApp').controller('OrganizeCtrl', OrganizeCtrl);

    OrganizeCtrl.$inject = ['$scope', 'dataService', '$location', '$state', 'helper', '$rootScope', 'sessionService', '$timeout'];

    function OrganizeCtrl($scope, dataService, $location, $state, helper, $rootScope, sessionService, $timeout) {


        //显示
        $scope.listAction = function() {
            $scope.getorderList();
            $scope.qywxGet();
        }

        //绑定企业微信
        $scope.bindqywx = function() {
            var partner_id = sessionService.get('tokenUser').CompanyId;
            var code = "https://qy.dhfapp.com/qy/index?cmd=bind_dhf&partner_id=" + partner_id;
            return dataService.webQrcodeGet({ code: code }).then(function(redata) {
                $timeout(function() {
                    $scope.bindqywxQrImage = "data:image/png;base64," + redata;
                });
                checkBindState();
            });
        }
        //检查绑定状态
        function checkBindState() {
            $scope.qywxGet().then(function() {
                if (!$scope.qywx) {
                    $timeout(checkBindState, 3000);
                };
            });
        }
        //查询绑定企业微信
        $scope.qywxGet = function() {
            return dataService.qyCorpGet().then(function(result) {
                if (result && result.qy_corp_id) {
                    $scope.qywx = result;
                }
            });
        }

        //获取列表
        $scope.getorderList = function() {
            // console.log("111111111111111111111");
            dataService.v1PartnerGet(false, true).then(function(list) {
                if (list) {
                    // console.log("list",list);
                    $scope.partner = list;
                }
            });
        }



        //修改订单数据
        $scope.partnerUpdate = function() {
            var data = {
                "logo": $scope.partner.logo,
                "name": $scope.partner.name,
                "desc": $scope.partner.desc,
                "address": $scope.partner.address,
                "tel": $scope.partner.tel,
                "partner_id": $scope.partner.partner_id
            }

            dataService.v1PartnerUpdate(data).then(function(retu) {
                if (retu) {
                    helper.myAlert("success ");
                } else {
                    helper.myAlert("error ");
                }
            });
        }
    }
})();